Those days I’m looking a new job related to Java and Flex… Seems that this is a popular interview question, I event didn’t encounter it in the ACE. But I was asked more than 3 times.
Talk about overloading in OOPS:
Overloading is between the methods with the same name, If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.
ActionScript 3 support overriding, But why it is not support overloading?
It’s a good question, and can be the answer of what’s the difference between Java and Flex?
Based on the Standard ECMA-262 (ECMAScript Language Specification), Action Script does not implement the overloading. and every method in the class is a Function object(property)
Since AS doesn’t support overloading, what can you do to implement the overloading?
As I known, Basically there are three way to handle the overload requirement:
1. Using optional parameters, such as:
getUser(userID:int = –1, userName: String = null):User {..}
2. Using rich parameters:
getUser(…args):User{
if(args.length == 0) {
return null;
}else if (args.length…)….
}
3. Using Object as the parameters, like:
getUser(arg: Object):User {
if(arg is String) {
return …. get user by the userName;
} else {
if (arg is int) {
return …get user by the userID;
}//end of if.
}
According those codes, you can see those methods all need very clear comments, and not easy for other to use them. in the comment we need to tell others the parameters type or sequence.
In fact, Since the AS does not support the overloading, and the comments need be very very clear, and usually, there will be two public methods: getUserByID(), getUserByName(), maybe a private or protected method using the above 3 ways to do the get user job.
Let the Flex thread sleep for a while <->
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.